<template>
  <div class="app-container customer-div">
      <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="waitMy">待我处理</el-menu-item>
      <el-menu-item index="my">我的</el-menu-item>
<!--      <el-menu-item index="closed">交易关闭</el-menu-item>-->
      <el-menu-item index="all" v-if="allPurchaseApplyPay">全部</el-menu-item>
    </el-menu>
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="采购订单号：">
           <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="供应商：">
          <el-select
                  clearable
                  v-model="listQuery.supplierId"
                  filterable
                  reserve-keyword
                  placeholder="请输入关键词"
                  style="width: 220px;">
                  <el-option
                          v-for="item in options2"
                          :key="item.supplierId"
                          :label="item.supplierName"
                          :value="item.supplierId">
                  </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="合同公司：">
              <el-input v-model="listQuery.fromCompany" style="width: 220px;" clearable></el-input>
          </el-form-item>
        <el-form-item label="申请状态：" >
          <el-select v-model="listQuery.applyState" filterable placeholder="请选择" clearable>
              <el-option :key="0" label="待采购员审核" :value="0"></el-option>
              <el-option :key="10" label="作废" :value="10"></el-option>
              <el-option :key="20" label="待采购产品负责人审核" :value="20"></el-option>
              <el-option :key="23" label="待采购部门负责人确认" :value="23"></el-option>
              <el-option :key="30" label="待财务会计审核" :value="30"></el-option>
              <el-option :key="32" label="待财务经理审核" :value="32"></el-option>
<!--              <el-option :key="35" label="待总经理确认" :value="35"></el-option>-->
              <el-option :key="40" label="待支付" :value="40"></el-option>
              <el-option :key="50" label="已支付" :value="50"></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="业务员：" >
              <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable style="width:150px">
                  <el-option
                          v-for="item in userOptionList"
                          :key="item.userId"
                          :label="item.userName"
                          :value="item.userId">
                  </el-option>
              </el-select>
          </el-form-item>
        <el-form-item label="申请日期：">
          <el-date-picker
                  :default-time="['00:00:00', '23:59:59']"
                  v-model="listQuery.createTime"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付日期：" >
          <el-date-picker
                  :default-time="['00:00:00', '23:59:59']"
                  v-model="listQuery.payTime"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
          <el-form-item label="付款方式：" >
              <el-select v-model="listQuery.applyPaymentCode" class="input-width" style="width:150px" placeholder="请选择">
                  <el-option value="cash" label="现汇" key="cash" ></el-option>
                  <el-option value="chengdui" label="承兑" key="chengdui" ></el-option>
                  <el-option value="cashBalance" label="现金余额" key="cashBalance" ></el-option>
                  <el-option value="chengduiBalance" label="承兑余额" key="chengduiBalance" ></el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="类型：" >
              <el-select v-model="listQuery.applyType" filterable placeholder="请选择" clearable style="width:150px">
                  <el-option key="order_pay" label="采购单付款" value="order_pay"></el-option>
                  <el-option key="advance_pay" label="供应商预付款" value="advance_pay"></el-option>
                  <el-option key="order_refund" label="退款" value="order_refund"></el-option>
              </el-select>
          </el-form-item>

        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
<!--          <el-form-item>-->
<!--              <el-button-->
<!--                      @click="showDialog4()"-->
<!--                      type="primary">-->
<!--                  新增预付款-->
<!--              </el-button>-->
<!--          </el-form-item>-->
      </el-form>
    </div>

      <!--数据列表-->
<!--      <div style="font-size:14px;line-height: 30px;padding:10px 0 ">-->
<!--          <span style="padding-left:20px;">未入库金额：</span><span style="font-weight: bold">{{statisticsAmount }}</span>-->
<!--          <span style="padding-left:20px;">余额：</span><span style="font-weight: bold">{{statisticsGoodsNum }}</span>-->
<!--      </div>-->
    <div class="table-container">
        <el-table
          :header-cell-style="{'text-align':'center'}"
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
            <el-table-column
                    label="供应商名称"
                    width="200">
                <template slot-scope="scope">
                    {{scope.row.supplierName}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="类型"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.applyType | fifType}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="合同公司"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.fromCompany}}
                </template>
            </el-table-column>
            <el-table-column
                    label="业务员"
                    width="100"
                    align="center">
                <template slot-scope="scope">
                    {{scope.row.userName}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="付款方式"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.thisPaymentCode | fifPaymentCode}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="申请付款金额"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.thisPayAmount}}
                </template>
            </el-table-column>
          <el-table-column  label="采购单号" width="150" align="center">
            <template slot-scope="scope">
              {{scope.row.orderSn}}
            </template>
          </el-table-column>

          <el-table-column
                  label="合同数量"
                  width="100"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.goodsAllNum}}
            </template>
          </el-table-column>
          <el-table-column
                  label="合同金额"
                  width="100"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.orderAmount}}
            </template>
          </el-table-column>

          <el-table-column
                  align="center"
                  label="已付款"
                  width="100">
            <template slot-scope="scope">
              {{scope.row.hasPaidAmount}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  label="未付款"
                  width="100">
            <template slot-scope="scope">
              {{scope.row.unPaidAmount}}
            </template>
          </el-table-column>
            <el-table-column
                    align="center"
                    label="入库数量"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.goodsInNum}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="未入库数量"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.goodsUnInNum}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="入库金额"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.goodsInAmount}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="未入库金额"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.goodsUnInAmount}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="承兑余额"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.chengduiBalance}}
                </template>
            </el-table-column>

            <el-table-column
                    align="center"
                    label="现金余额"
                    width="100">
                <template slot-scope="scope">
                    {{scope.row.cashBalance}}
                </template>
            </el-table-column>

        <el-table-column
                align="center"
                label="申请付款时间"
                width="100">
            <template slot-scope="scope">
                {{scope.row.thisPayTime}}
            </template>
        </el-table-column>
          <el-table-column
                  align="center"
                  label="申请付款备注"
                  width="200">
            <template slot-scope="scope">
              {{scope.row.thisPayRemark}}
            </template>
          </el-table-column>
            <el-table-column  label="申请时间" width="150" align="center">
                <template slot-scope="scope">
                    {{scope.row.applyCreateTime}}
                </template>
            </el-table-column>
          <el-table-column
                  align="center"
                  label="申请状态"
                  width="100">
            <template slot-scope="scope">
              {{scope.row.applyState | fifterState}}
            </template>
          </el-table-column>

          <el-table-column
                  align="center"
                  label="实付金额"
                  width="100">
            <template slot-scope="scope">
              {{scope.row.actualPayAmount}}
            </template>
          </el-table-column>

          <el-table-column
                  align="center"
                  label="付款时间"
                  width="150">
            <template slot-scope="scope">
              {{scope.row.actualPayTime}}
            </template>
          </el-table-column>

          <el-table-column
                  fixed="right"
                  align="center"
                  label="操作"
                  width="100">
              <template slot-scope="scope">
                  <el-button type="text" @click="toInfo(scope.row)">查看</el-button>
<!--                  <el-button type="text" @click="shenhe(scope.row,'purchaseReCheck')" v-if="scope.row.applyState == 20 && scope.row.purchaseUserId == userId">审核</el-button>-->
<!--                  <el-button type="text" @click="showDialog3(scope.row)" v-if="scope.row.applyState == 0 && scope.row.userId == userId " v-show="scope.row.thisPaymentCode == 'cash' || scope.row.thisPaymentCode == 'chengdui'">修改申请</el-button>-->
<!--                  <el-button type="text" @click="showCancel(scope.row)" v-if="scope.row.applyState == 0 && scope.row.userId == userId ">取消申请</el-button>-->
              </template>
          </el-table-column>
        </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

      <!--申请支付-->
      <el-dialog
              :close-on-click-modal="false"
              :beforeClose="handleClose3"
              title="申请付款"
              :visible.sync="dialogVisible3"
              width="400px">
          <el-form :model="appleForm" size="mini" label-width="140px"  ref="appleForm" :rules="rules3">
              <el-form-item label="供应商：" >{{appleForm.supplierName}}</el-form-item>
              <el-form-item label="合同公司：" >{{appleForm.fromCompany}}</el-form-item>
              <el-form-item label="合同金额：" >{{appleForm.orderAmount}}</el-form-item>
              <el-form-item label="已支付金额：" >{{appleForm.hasPaidAmount}}</el-form-item>
              <el-form-item label="未付款金额：">{{appleForm.unPaidAmount}}</el-form-item>
              <el-form-item label="可申请金额：">{{appleForm.canApplyAmount}}</el-form-item>
              <el-form-item label="付款方式：" prop="applyPaymentCode" >
                  <el-select v-model="appleForm.applyPaymentCode" class="input-width" style="width:195px" placeholder="请选择" disabled>
                      <el-option value="cash" label="现汇" key="cash" ></el-option>
                      <el-option value="chengdui" label="承兑" key="chengdui" ></el-option>
                      <el-option value="cashBalance" :label="'现金余额'+'('+appleForm.CashBalance+')'" key="cashBalance" ></el-option>
                      <el-option value="chengduiBalance" :label="'承兑余额'+'('+appleForm.ChengduiBalance+')'" key="chengduiBalance" ></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="申请付款金额：" prop="applyPayAmount">
                  <el-input v-model="appleForm.applyPayAmount"></el-input>
              </el-form-item>
              <el-form-item label="付款时间：" prop="applyPayTime">
                  <el-date-picker
                          v-model="appleForm.applyPayTime"
                          value-format="yyyy-MM-dd HH:mm"
                          type="datetime"
                          placeholder="选择日期时间">
                  </el-date-picker>
              </el-form-item>

              <el-form-item label="付款备注：">
                  <el-input v-model="appleForm.applyPayRemark" :autosize="{ minRows: 2, maxRows: 6}" type="textarea"></el-input>
              </el-form-item>
          </el-form>
          <div style="color:red;padding:0 16px;font-size:12px">注：选择承兑支付时，请确认已有承兑票据</div>
          <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose3">取 消</el-button>
              <el-button type="primary" @click="handleConfirm3" :loading="btnLoading3">确 定</el-button>
          </span>
      </el-dialog>


      <!--新增预付款-->
      <el-dialog
              :close-on-click-modal="false"
              :beforeClose="handleClose4"
              title="新增预付款"
              :visible.sync="dialogVisible4"
              width="400px">
          <el-form :model="appleForm2" size="mini" label-width="140px"  ref="appleForm2" :rules="rules4">
              <el-form-item label="供应商：" prop="supplierId">
                  <el-select
                          @change="changeSupplierId"
                          v-model="appleForm2.supplierId"
                          filterable
                          reserve-keyword
                          placeholder="请输入关键词">
                      <el-option
                              v-for="item in options2"
                              :key="item.supplierId"
                              :label="item.supplierName"
                              :value="item.supplierId">
                      </el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="合同公司：" prop="fromCompany">
                  <el-select v-model="appleForm2.fromCompany" class="input-width" style="width:195px" placeholder="请选择"  @change="changeBalance">
                      <el-option value="普利成" label="普利成" key="普利成" ></el-option>
                      <el-option value="塑盟" label="塑盟" key="塑盟" ></el-option>
                      <el-option value="泛塑" label="泛塑" key="泛塑" ></el-option>
                      <el-option value="华灯车尚" label="华灯车尚" key="华灯车尚"></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="付款方式：" prop="applyPaymentCode" >
                  <el-select v-model="appleForm2.applyPaymentCode" class="input-width" style="width:195px" placeholder="请选择" @change="changeBalance">
                      <el-option value="cash" label="现汇" key="cash" ></el-option>
                      <el-option value="chengdui" label="承兑" key="chengdui" ></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="预付款余额：" >{{appleForm2.balance}}</el-form-item>
<!--              <el-form-item label="类型：" >-->
<!--                  <el-select v-model="appleForm2.applyPaymentCode" class="input-width" style="width:195px" placeholder="请选择" >-->
<!--                      <el-option value="cash" label="预付款" key="cash" ></el-option>-->
<!--                  </el-select>-->
<!--              </el-form-item>-->
              <el-form-item label="申请付款金额：" prop="applyPayAmount">
                  <el-input v-model="appleForm2.applyPayAmount"></el-input>
              </el-form-item>
              <el-form-item label="付款时间：" prop="applyPayTime">
                  <el-date-picker
                          :picker-options="pickerOptions"
                          v-model="appleForm2.applyPayTime"
                          value-format="yyyy-MM-dd HH:mm"
                          type="datetime"
                          placeholder="选择日期时间">
                  </el-date-picker>
              </el-form-item>

              <el-form-item label="付款备注：">
                  <el-input v-model="appleForm2.applyPayRemark" :autosize="{ minRows: 2, maxRows: 6}" type="textarea"></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose4">取 消</el-button>
              <el-button type="primary" @click="handleConfirm4" :loading="btnLoading4">确 定</el-button>
          </span>
      </el-dialog>

      <!--回退弹窗-->
      <el-dialog
              :close-on-click-modal="false"
              title="审核回退"
              :visible.sync="dialogVisible2"
              width="500px">
          <el-form :model="passForm" size="mini" label-width="100px" ref="passForm" :rules="rules2">
              <el-form-item label="备注信息：" prop="remark">
                  <el-input :autosize="{ minRows: 2, maxRows: 6}" type="textarea" v-model="passForm.remark"></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button  @click="dialogVisible2 = false">取消</el-button>
              <el-button @click="shenhe(curRow,'back')" :loading="btnLoading2" type="primary">确定</el-button>
          </span>
      </el-dialog>

  </div>
</template>
<script>
  import {getCustomerOption,getOrderUsersList,purchaseOrderList,supplierList,purchaseAddApplyPay,purchaseApplyPayList,applyPayProcess,supplierBalanceInfo,addAdvancePay} from '@/api/partner'
  import {goodsList} from '@/api/company'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  import {validateCommission} from '@/utils/validate';

  const defaultListQuery={
    "orderSn"     : "",    //采购单号
    "supplierId"  : "",     //供应商ID
    "createTime"  : '',  //申请时间
    "applyState"  : "", //申请状态
    "payTime" : '', //实际支付时间
    "userId" : '', //业务员ID
    "pageNum" : 1,
    "pageSize" : 10,
    tab:'waitMy',
    fromCompany:'',
    applyType:''
  };
  const defaultAppleForm={
    "orderId"     : '',    //采购单ID
    "applyPaymentCode" : "",  //支付方式
    "applyPayTime"   : "",    //支付时间
    "applyPayAmount" : '',  //支付金额
    "applyPayRemark" : "" , //备注
    orderAmount:'',
    unPaidAmount:'',
    hasPaidAmount:'',
    canApplyAmount:'',
      fromCompany:'',
      supplierName:'',
      CashBalance:'',
      ChengduiBalance:''
  };

  const defaultAppleForm2={
      "applyPaymentCode" : "",  //支付方式
      "applyPayTime"   : "",    //支付时间
      "applyPayAmount" : '',  //支付金额
      "applyPayRemark" : "" , //备注
      fromCompany:'',
      supplierId:'',
      balance:''
  };
  export default {
    name: "index",
    data(){
        const isvalidateApplyPayAmount = (rule, value, callback) => {
            if (!validateCommission(parseFloat(value))) {
                callback(new Error('非负整数或小数'))
            } else if(this.appleForm.applyPaymentCode == 'cash' && value > this.appleForm.canApplyAmount){
                callback(new Error('超过可申请金额'))
            }else if(this.appleForm.applyPaymentCode == 'chengdui' &&  this.appleForm.canApplyAmount <= 0){
                callback(new Error('可申请金额小于等于0，不可以再次申请'))
            }else if(this.appleForm.applyPaymentCode == 'cashBalance' && value > this.appleForm.canApplyAmount){
                callback(new Error('超过可申请金额'))
            }else if(this.appleForm.applyPaymentCode == 'cashBalance' && value > this.appleForm.CashBalance){
                callback(new Error('超过现金余额'))
            }else if(this.appleForm.applyPaymentCode == 'chengduiBalance' && value > this.appleForm.canApplyAmount){
                callback(new Error('超过可申请金额'))
            }else if(this.appleForm.applyPaymentCode == 'chengduiBalance' && value > this.appleForm.ChengduiBalance){
                callback(new Error('超过承兑余额'))
            }else {
                callback()
            }
        };
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        userOptionList:[],
        roles:store.getters.roles ? store.getters.roles : [],
        loading:false,
        options:[],
        loading2:false,
        options2:[],
        stateList:[
          {label:'待采购员审核',val:0},
          {label:'作废',val:10},
          {label:'待产品负责人审核',val:20},
          {label:'待总经理审核',val:25},
          {label:'流程结束',val:40},
          {label:'交易关闭',val:50},
        ],
        statisticsAmount:'',
        statisticsGoodsNum:'',
        dialogVisible3:false,
        rules3:{
          applyPaymentCode:[{required: true, trigger: 'change' ,message: '请选择支付方式',}],
          applyPayTime:[{required: true, trigger: 'change' ,message: '请选择支付时间',}],
          applyPayAmount:[{required: true, trigger: 'blur' ,validator:isvalidateApplyPayAmount,}],
          },
       appleForm:JSON.parse(JSON.stringify(defaultAppleForm)),
       btnLoading3:false,
          btnLoading2:false,
          dialogVisible2:false,
          passForm:{
              remark:'',
          },
          rules2:{
              remark:[{required: true, trigger: 'blur' ,message: '请输入回退原因',}],
          },
          curRow:{},

          dialogVisible4:false,
          rules4:{
              supplierId:[{required: true, trigger: 'change' ,message: '请选择供应商',}],
              fromCompany:[{required: true, trigger: 'change' ,message: '请选择合同公司',}],
              applyPaymentCode:[{required: true, trigger: 'change' ,message: '请选择支付方式',}],
              applyPayTime:[{required: true, trigger: 'change' ,message: '请选择支付时间',}],
              applyPayAmount:[{required: true, trigger: 'blur' ,message: '请输入申请金额',}],
          },
          appleForm2:JSON.parse(JSON.stringify(defaultAppleForm)),
          btnLoading4:false,
          balance:{},
          pickerOptions: {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),
      allPurchaseApplyPay (){
            if(this.roles.indexOf("allPurchaseApplyPay") != -1){
                return true
            }else{
                return false
            }
        },
    },
    created() {
      if(this.$route.query.tab){
        this.listQuery.tab = this.$route.query.tab
      }
      this.getList();
      this.getUserOptionList();
      this.getSupplierList()
    },
    filters: {
      formatCreateTime(time) {
        if(time){
          let date = new Date(time);
          return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
        }else{
          return '--'
        }
      },
      fiferPayState(val){
        if(val == 0){
          return '未付款'
        }else if(val == 1){
          return '部分付款'
        }else if(val == 2){
          return '已付款'
        }
      },
      fifterState(val){
        if(val == 0){
          return '待采购员审核'
        }else if(val == 10){
          return '作废'
        }else if(val == 20){
          return '待产品负责人审核'
        }else if(val == 23){
          return '待采购部门负责人确认'
        }else if(val == 30){
          return '待财务会计审核'
        }else if(val == 32){
          return '待财务经理审核'
        }else if(val == 35){
          return '待总经理确认'
        }else if(val == 40){
          return '待支付'
        }else if(val == 50){
          return '已支付'
        }
      },
      fifPaymentCode(val){
        if(val == 'cash'){
          return '现汇'
        }else if(val == 'chengdui'){
          return '承兑'
        }
      },
        fifType(val){
            if(val == 'advance_pay'){
                return '预付款'
            }else if(val == 'order_pay'){
                return '采购付款'
            }else if(val == 'order_refund'){
                return '退款'
            }
        },
    },
    methods:{
        //切换充值供应商
        changeSupplierId(){
            supplierBalanceInfo({supplierId:this.appleForm2.supplierId}).then((res)=>{
                this.balance = res;
                this.changeBalance();
            });
        },
        //切换余额
        changeBalance(){
            if(this.appleForm2.supplierId && this.appleForm2.fromCompany && this.appleForm2.applyPaymentCode){
                if(this.appleForm2.fromCompany.indexOf('普利成') != -1){
                     this.appleForm2.balance = this.appleForm2.applyPaymentCode == 'cash' ? this.balance.polisenCashBalance : this.balance.polisenChengduiBalance;
                }else if(this.appleForm2.fromCompany.indexOf('塑盟') != -1){
                    this.appleForm2.balance = this.appleForm2.applyPaymentCode == 'cash' ? this.balance.sumengCashBalance : this.balance.sumengChengduiBalance;
                }else if(this.appleForm2.fromCompany.indexOf('泛塑') != -1){
                    this.appleForm2.balance = this.appleForm2.applyPaymentCode == 'cash' ? this.balance.fansuCashBalance : this.balance.fansuChengduiBalance;
                }
            }
        },
        //显示充值弹窗
        showDialog4(){
            this.dialogVisible4 = true;
            this.appleForm2 = JSON.parse(JSON.stringify(defaultAppleForm2));
        },
        //关闭充值弹窗
        handleClose4(){
            this.dialogVisible4 = false;
            this.$refs.appleForm2.clearValidate();
        },
        //提交充值
        handleConfirm4(){
            this.$refs['appleForm2'].validate((valid) => {
                if (valid) {
                    this.btnLoading4 = true;
                    let data = {};
                    data.supplierId = this.appleForm2.supplierId;
                    data.fromCompany = this.appleForm2.fromCompany;
                    data.applyPaymentCode = this.appleForm2.applyPaymentCode;
                    data.applyPayTime = this.appleForm2.applyPayTime;
                    data.applyPayAmount = this.appleForm2.applyPayAmount;
                    data.applyPayRemark = this.appleForm2.applyPayRemark;
                    addAdvancePay(data).then(()=>{
                        this.btnLoading4 = false;
                        this.dialogVisible4 = false;
                        this.getList()
                    }).catch(()=>{
                        this.btnLoading4 = false;
                    })
                }
            })
        },

        //显示回退弹窗
        showCancel(item){
            this.dialogVisible2 = true;
            this.passForm.remark = '';
            this.curRow = JSON.parse(JSON.stringify(item));
        },
        //审核
        shenhe(item,type){
            let key;
            let data = {};
            data.applyId= item.applyId;
            data.action = type;
            if(type == 'purchaseReCheck'){
                key = 'btnLoading';
                this[key] = true;
                this.submitPass(data,key)
            }else if(type == 'back'){
                key = 'btnLoading2';
                data.remark = this.passForm.remark;
                this.$refs.passForm.validate((valid) => {
                    if (valid) {
                        this[key] = true;
                        this.submitPass(data,key)
                    }
                });
            }
        },
        submitPass(data,key){
            applyPayProcess(data).then(()=>{
                this.$message({
                    message: '操作成功！',
                    type: 'success'
                });
                this[key] = false;
                this.dialogVisible2 = false;
                this.getList()
            }).catch(()=>{
                this[key] = false;
            })
        },

        //显示申请付款弹窗
        showDialog3(item){
            this.dialogVisible3 = true;
            supplierBalanceInfo({supplierId:item.supplierId}).then((res)=>{
                if(item.fromCompany.indexOf('普利成') != -1){
                    this.appleForm.CashBalance = res.polisenCashBalance;
                    this.appleForm.ChengduiBalance = res.polisenChengduiBalance;
                }else if(item.fromCompany.indexOf('塑盟') != -1){
                    this.appleForm.CashBalance = res.sumengCashBalance;
                    this.appleForm.ChengduiBalance = res.sumengChengduiBalance;
                }else if(item.fromCompany.indexOf('泛塑') != -1){
                    this.appleForm.CashBalance = res.fansuCashBalance;
                    this.appleForm.ChengduiBalance = res.fansuChengduiBalance;
                }
                this.appleForm.currentPrePayAmount = res.currentPrePayAmount
            });
            this.appleForm.applyId = item.applyId;
            this.appleForm.orderId = item.orderId;
            this.appleForm.applyPaymentCode = item.thisPaymentCode;
            this.appleForm.applyPayTime = item.thisPayTime;
            this.appleForm.applyPayAmount = item.thisPayAmount;
            this.appleForm.applyPayRemark = item.thisPayRemark;
            this.appleForm.orderAmount = item.orderAmount;
            this.appleForm.unPaidAmount = item.unPaidAmount;
            this.appleForm.hasPaidAmount = item.hasPaidAmount;
            this.appleForm.canApplyAmount = item.canApplyAmount;
        },
        //关闭申请付款弹窗
        handleClose3(){
            this.dialogVisible3 = false;
            this.$refs.appleForm.clearValidate();
        },
        //提交申请付款
        handleConfirm3(){
            this.$refs['appleForm'].validate((valid) => {
                if (valid) {
                    if(this.appleForm.applyPaymentCode == 'chengdui' &&  this.appleForm.canApplyAmount <= 0){
                        this.$message({
                            message: '承兑支付时可申请金额小于等于0，不可以再次申请',
                            type: 'error'
                        });
                        return false
                    }
                    this.btnLoading3 = true;
                    let data = {};
                    data.applyId = this.appleForm.applyId;
                    data.orderId = this.appleForm.orderId;
                    data.applyPaymentCode = this.appleForm.applyPaymentCode;
                    data.applyPayTime = this.appleForm.applyPayTime;
                    data.applyPayAmount = this.appleForm.applyPayAmount;
                    data.applyPayRemark = this.appleForm.applyPayRemark;
                    purchaseAddApplyPay(data).then(()=>{
                          this.btnLoading3 = false;
                          this.dialogVisible3 = false;
                          this.getList()
                      }).catch(()=>{
                          this.btnLoading3 = false;
                      })
                }
            })
        },
      //去详情页
      toInfo(item){
        let routeData = this.$router.resolve({name:'purchaseApplyPayInfo',query:{applyId:item.applyId,applyType:item.applyType}});
        window.open(routeData.href);
      },
      //商品
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;
          this.loading = false;
          goodsList({keywords:query}).then((res)=>{
            this.options = res.list;
          })
        } else {
          this.options = [];
        }
      },

      //供应商
      getSupplierList() {
          supplierList().then((res)=>{
            this.options2 = res;
          })
      },

      //获取员工下拉选项
      getUserOptionList(){
        getOrderUsersList().then((res)=>{
          this.userOptionList = res;
        })
      },

      //选择员工名称
      handleSelectUserName(key,item){
        this.listQuery[key] = item.userId;
      },

      //客户名称输入匹配
      querySearch(queryString, cb){
        getCustomerOption({keywords:queryString}).then((res)=>{
          cb(res);
        })
      },

      //选择客户名称
      handleSelectCustomerName(item){
        this.listQuery.keywords = item.customerName
      },

      /**
       * 切换选择状态
       */
      handleSelect(key, keyPath){
        this.listQuery.tab = key;
        this.handleSearchList();
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取订单列表
      getList() {
        this.listLoading = true;
        purchaseApplyPayList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

    },
  }
</script>

<style scoped lang="scss">

</style>
